# -*- coding: utf-8 -*-
from flask import Flask, request
from flask.ext.cors import CORS
import json

import BLL.GoodsType
import BLL.Gallery
import BLL.Sys
import BLL.FileUp

import sys

reload(sys)
sys.setdefaultencoding("utf-8")

app = Flask(__name__)
CORS(app)  # 解决跨域问题

@app.route('/')
def hello_world():
    return 'Hello World!'


def FF_InputPostGet(strIn):
    try:
        if "data" in strIn:
            _request = eval(strIn.get('data'))
        else:
            for xb in strIn:
                xc = xb
            if xc[0] == '{':
                _request = json.loads(xc)
            else:
                xd = '{' + xc + '}'
                _request = json.loads(xd)
        return _request
    except:
        _request = "JSON解析失败"
        return _request


@app.route('/api/document')
def _api_get_api_document_html():
    request = '请点击 </br>' \
              ' <a href='"/" + "api" + "/" + "gallery.html"'>产品类型</a> </br>' \
                                             ' <a href='"/" + "api" + "/" + "goodstype.html"'>展厅信息</a> </br>' ' <a href='"/" + "api" + "/" + "fileup.html"'>图片上传</a> </br>'
    return request


@app.route('/api/gallery.html')
def _api_get_api_gallery_html():
    objReturn = BLL.Sys._api_get_api_gallery_html()
    return objReturn

@app.route('/api/fileup.html')
def _api_get_api_fileup_html():
    objReturn = BLL.Sys._api_get_api_fileup_html()
    return objReturn

@app.route('/api/goodstype.html')
def _api_get_api_goodstype_html():
    objReturn = BLL.Sys._api_get_api_goodsType_html()
    return objReturn


# -------------------------------------------------------------------------------------------------------------------------------------------------------------------

# 查询所有展厅信息
@app.route('/api/gallery.select.all', methods=["GET", "POST"])
def _api_gallery_select_all():
    if request.method == "POST":
        _request = FF_InputPostGet(request.form)
        objReturn = BLL.Gallery._api_gallery_select_all(_request)
        _response = json.dumps(objReturn, encoding="UTF-8", ensure_ascii=False)
        return _response
    else:
        pass


# 修改---修改展厅编码对应的展厅名称
@app.route('/api/gallery.bycode.name.edit', methods=["GET", "POST"])
def _api_gallery_bycode_name_edit():
    if request.method == "POST":
        _request = FF_InputPostGet(request.form)
        objReturn = BLL.Gallery._api_gallery_bycode_name_edit(_request)
        _response = json.dumps(objReturn, encoding="UTF-8", ensure_ascii=False)
        return _response
    else:
        pass


# 修改---展厅介绍信息
@app.route('/api/gallery.infor.edit', methods=["GET", "POST"])
def _api_gallery_infor_edit():
    if request.method == "POST":
        _request = FF_InputPostGet(request.form)
        objReturn = BLL.Gallery._api_gallery_infor_edit(_request)
        _response = json.dumps(objReturn, encoding="UTF-8", ensure_ascii=False)
        return _response
    else:
        pass


# 添加---展厅图片
@app.route('/api/gallery.picture.add', methods=["GET", "POST"])
def _api_gallery_picture_add():
    if request.method == "POST":
        _request = FF_InputPostGet(request.form)
        objReturn = BLL.Gallery._api_gallery_picture_add(_request)
        _response = json.dumps(objReturn, encoding="UTF-8", ensure_ascii=False)
        return _response
    else:
        pass


# 查询---根据展厅编号查询展厅信息（展厅首页信息展示）
@app.route('/api/gallery.select.bycode', methods=["GET", "POST"])
def _api_gallery_select_bycode():
    if request.method == "POST":
        _request = FF_InputPostGet(request.form)
        objReturn = BLL.Gallery._api_gallery_select_bycode(_request)
        _response = json.dumps(objReturn, encoding="UTF-8", ensure_ascii=False)
        return _response
    else:
        pass


# 删除---删除展厅主页图片
@app.route('/api/gallery.picture.delete', methods=["GET", "POST"])
def _api_gallery_picture_delete():
    if request.method == "POST":
        _request = FF_InputPostGet(request.form)
        objReturn = BLL.Gallery._api_gallery_picture_delete(_request)
        _response = json.dumps(objReturn, encoding="UTF-8", ensure_ascii=False)
        return _response
    else:
        pass

# 添加--展厅信息
@app.route('/api/gallery.add', methods=["GET", "POST"])
def _api_gallery_add():
    if request.method == "POST":
        _request = FF_InputPostGet(request.form)
        objReturn = BLL.Gallery._api_gallery_add(_request)
        _response = json.dumps(objReturn, encoding="UTF-8", ensure_ascii=False)
        return _response
    else:
        pass

@app.route('/api/gallery.select.bygallerycode', methods=["GET", "POST"])
def _api_gallery_select_bygallerycode():
    if request.method == "POST":
        _request = FF_InputPostGet(request.form)
        objReturn = BLL.Gallery._api_gallery_select_bygallerycode(_request)
        _response = json.dumps(objReturn, encoding="UTF-8", ensure_ascii=False)
        return _response
    else:
        pass

# 查询---根据展厅编号查询展厅信息（展厅首页信息展示）V2
@app.route('/api/gallery.select.bycode.v2', methods=["GET", "POST"])
def _api_gallery_select_bycode_v2():
    if request.method == "POST":
        _request = FF_InputPostGet(request.form)
        objReturn = BLL.Gallery._api_gallery_select_bycode_v2(_request)
        _response = json.dumps(objReturn, encoding="UTF-8", ensure_ascii=False)
        return _response
    else:
        pass

# 查询---根据展厅标识查询展厅信息（展厅首页信息展示）V2
@app.route('/api/gallery.select.bygallerycode.v2', methods=["GET", "POST"])
def _api_gallery_select_bygallerycode_v2():
    if request.method == "POST":
        _request = FF_InputPostGet(request.form)
        objReturn = BLL.Gallery._api_gallery_select_bygallerycode_v2(_request)
        _response = json.dumps(objReturn, encoding="UTF-8", ensure_ascii=False)
        return _response
    else:
        pass

# 添加---展厅图片V2
@app.route('/api/gallery.picture.add.v2', methods=["GET", "POST"])
def _api_gallery_picture_add_v2():
    if request.method == "POST":
        _request = FF_InputPostGet(request.form)
        objReturn = BLL.Gallery._api_gallery_picture_add_v2(_request)
        _response = json.dumps(objReturn, encoding="UTF-8", ensure_ascii=False)
        return _response
    else:
        pass

# 修改--展厅对应图片信息V2
@app.route('/api/gallery.picture.information.edit.v2', methods=["GET", "POST"])
def _api_gallery_picture_information_edit_v2():
    if request.method == "POST":
        _request = FF_InputPostGet(request.form)
        objReturn = BLL.Gallery._api_gallery_picture_information_edit_v2(_request)
        _response = json.dumps(objReturn, encoding="UTF-8", ensure_ascii=False)
        return _response
    else:
        pass



# --------------------------------------------------------------------------------------------------------------------------------------------

# 查询--类别信息
@app.route('/api/goodstype.select.all', methods=["GET", "POST"])
def _api_goodstype_select_all():
    if request.method == "POST":
        _request = FF_InputPostGet(request.form)
        print _request
        objReturn = BLL.GoodsType._api_goodstype_select_all(_request)
        _response = json.dumps(objReturn, encoding="UTF-8", ensure_ascii=False)
        return _response
    else:
        pass


# 添加---类型对应商品图片
@app.route('/api/goodstype.type.picture.add', methods=["GET", "POST"])
def _api_goodstype_type_picture_add():
    if request.method == "POST":
        _request = FF_InputPostGet(request.form)
        objReturn = BLL.GoodsType._api_goodstype_type_picture_add(_request)
        _response = json.dumps(objReturn, encoding="UTF-8", ensure_ascii=False)
        return _response
    else:
        pass


# 查询---类型对应商品图片
@app.route('/api/goodstype.type.picture.select', methods=["GET", "POST"])
def _api_goodstype_type_picture_select():
    if request.method == "POST":
        _request = FF_InputPostGet(request.form)
        objReturn = BLL.GoodsType._api_goodstype_type_picture_select(_request)
        _response = json.dumps(objReturn, encoding="UTF-8", ensure_ascii=False)
        return _response
    else:
        pass

# 删除---类型对应商品图片
@app.route('/api/goodstype.type.picture.delete', methods=["GET", "POST"])
def _api_goodstype_type_picture_delete():
    if request.method == "POST":
        _request = FF_InputPostGet(request.form)
        objReturn = BLL.GoodsType._api_goodstype_type_picture_delete(_request)
        _response = json.dumps(objReturn, encoding="UTF-8", ensure_ascii=False)
        return _response
    else:
        pass

# 新增---类型，图片（类型不存在时使用）
@app.route('/api/goodstype.type.add', methods=["GET", "POST"])
def _api_goodstype_type_add():
    if request.method == "POST":
        _request = FF_InputPostGet(request.form)
        objReturn = BLL.GoodsType._api_goodstype_type_add(_request)
        _response = json.dumps(objReturn, encoding="UTF-8", ensure_ascii=False)
        return _response
    else:
        pass

#新增--类型（只添加类型，不添加图片）
@app.route('/api/goodstype.add', methods=["GET", "POST"])
def _api_goodstype_add():
    if request.method == "POST":
        _request = FF_InputPostGet(request.form)
        objReturn = BLL.GoodsType._api_goodstype_add(_request)
        _response = json.dumps(objReturn, encoding="UTF-8", ensure_ascii=False)
        return _response
    else:
        pass

# 查询--类型模糊查询
@app.route('/api/goodstype.type.mohu.select', methods=["GET", "POST"])
def _api_goodstype_type_mohu_select():
    if request.method == "POST":
        _request = FF_InputPostGet(request.form)
        objReturn = BLL.GoodsType._api_goodstype_type_mohu_select(_request)
        _response = json.dumps(objReturn, encoding="UTF-8", ensure_ascii=False)
        return _response
    else:
        pass

# 修改--类型对应图片信息
@app.route('/api/goodstype.type.picture.information.edit', methods=["GET", "POST"])
def _api_goodstype_type_picture_information_edit():
    if request.method == "POST":
        _request = FF_InputPostGet(request.form)
        objReturn = BLL.GoodsType._api_goodstype_type_picture_information_edit(_request)
        _response = json.dumps(objReturn, encoding="UTF-8", ensure_ascii=False)
        return _response
    else:
        pass

# 修改---图片信息
@app.route('/api/gallery.picture.information.edit', methods=["GET", "POST"])
def _api_gallery_picture_information_edit():
    if request.method == "POST":
        _request = FF_InputPostGet(request.form)
        objReturn = BLL.Gallery._api_gallery_picture_information_edit(_request)
        _response = json.dumps(objReturn, encoding="UTF-8", ensure_ascii=False)
        return _response
    else:
        pass

# 停用---类型
@app.route('/api/goodstype.type.del', methods=["GET", "POST"])
def _api_goodstype_type_del():
    if request.method == "POST":
        _request = FF_InputPostGet(request.form)
        objReturn = BLL.GoodsType._api_goodstype_type_del(_request)
        _response = json.dumps(objReturn, encoding="UTF-8", ensure_ascii=False)
        return _response
    else:
        pass
# 编辑---类型
@app.route('/api/goodstype.type.edit', methods=["GET", "POST"])
def _api_goodstype_type_edit():
    if request.method == "POST":
        _request = FF_InputPostGet(request.form)
        objReturn = BLL.GoodsType._api_goodstype_type_edit(_request)
        _response = json.dumps(objReturn, encoding="UTF-8", ensure_ascii=False)
        return _response
    else:
        pass
# --------------------------------------------------------------------------------------------------------------------------------------------

# @app.route('/api/fileup.image', methods=["GET", "POST"])
# def _api_imageup_image():
#     if request.method == 'POST':
#         file = request.files['file']
#         objReturn = BLL.FileUp._api_img_up_01(file)
#         _response = json.dumps(objReturn, encoding="UTF-8", ensure_ascii=False)
#         return _response
#     else:
#         return "接口--上传图片"

# 上传图片--新增展厅的图片
@app.route('/api/fileup.image.byshop', methods=["GET", "POST"])
def _api_imageup_image_byShop():
    # GalleryNum  展厅编号
    # ImgRemark     图片备注
    # ImgType    图片类型
    # ImgName     图片名称
    # ImgPath     图片路径
    # GoodsTypeID
    if request.method == 'POST':
        file = request.files['file']

        cs_GalleryNum = request.args.get("GalleryNum")
        cs_ImgRemark = request.args.get("ImgRemark")
        cs_ImgType = request.args.get("ImgType")
        cs_ImgName = request.args.get("ImgName")
        cs_GoodsTypeIDx = request.args.get("GoodsTypeID")

        objReturn = BLL.FileUp._api_img_up_byshop(file, cs_GalleryNum,cs_ImgRemark,cs_ImgType,cs_ImgName ,cs_GoodsTypeIDx)



        _response = json.dumps(objReturn, encoding="UTF-8", ensure_ascii=False)
        return _response
    else:
        return "接口--上传图片--新增展厅的图片"


# 上传图片--新增产品类别的图片
@app.route('/api/fileup.image.bytype', methods=["GET", "POST"])
def _api_imageup_image_bytype():
    if request.method == 'POST':
        file = request.files['file']

        cs_ImgRemark = request.args.get("ImgRemark")
        cs_GoodsTypeID = request.args.get("GoodsTypeID")
        cs_ImgName = request.args.get("ImgName")

        objReturn = BLL.FileUp._api_img_up_bytype(file,cs_ImgRemark,cs_GoodsTypeID,cs_ImgName)
        _response = json.dumps(objReturn, encoding="UTF-8", ensure_ascii=False)
        return _response
    else:
        return "接口--上传图片--新增产品类别的图片"

# -----------------------------------------------------------------------------------s---------------------------------------------------------


if __name__ == '__main__':
    # app.run()
    app.run(host='0.0.0.0',port=5001)